# coding:utf8
from ..models import *
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, ValidationError
from wtforms.validators import DataRequired


class LoginForm(FlaskForm):
    account = StringField(
        label='帐号',
        validators=[
            DataRequired('请输入帐号!')
        ],
        description='帐号',
        render_kw={
            "class": "form-control",
            "placeholder": "请输入账号！",
            "required": "required"
        }
    )

    pwd = PasswordField(
        label='密码',
        validators=[
            DataRequired('请输入密码')
        ],
        description='密码',
        render_kw={
            "class": "form-control",
            "placeholder": "请输入密码！",
            "required": "required"
        }
    )

    submit = SubmitField(
        label='登录',
        render_kw={
            "class": "btn btn-primary btn-block btn-flat",
            "id": "btn-sub"
        }
    )

    def validate_account(self, field):
        account = field.data
        admin = Admin.query.filter_by(name=account).count()
        if admin == 0:
            raise ValidationError('帐号不存在')


class TagForm(FlaskForm):
    name = StringField(
        label='名称',
        validators=[
            DataRequired('请输入标签名称!')
        ],
        description='标签名称',
        render_kw={
            "class": "form-control",
            "id": "input_name",
            "placeholder": "请输入标签名称！",
            # "required": "required"
        }
    )

    submit = SubmitField(
        label='添加',
        render_kw={
            "class": "btn btn-primary",
            "id": "btn-sub"
        }
    )

    @staticmethod
    def check_exists_name(name):
        count_tag = Tag.query.filter_by(name=name).count()
        if int(count_tag) > 0:
            return True
        return False
